//
//  BookingListViewController.m
//  homeland
//
//  Created by SunShine on 14-1-25.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "BookingListViewController.h"
#import "WetlandService.h"
#import "BookingOrderListCell.h"
#import "WetLandOrderDetailViewController.h"

@interface BookingListViewController ()
@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *table;
@property (strong, nonatomic) CustomPullToRefresh *ptr;

@property (strong, nonatomic) NSArray *dataList;
@property (assign, nonatomic) NSInteger pageNo;
-(NSString*)getState:(NSString*)state;
@end

@implementation BookingListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"预约记录";
    self.searchBar.inputAccessoryView = [self getInputAccessoryView];
    // Do any additional setup after loading the view from its nib.
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.table delegate:self];
}
-(void)viewWillAppear:(BOOL)animated{
    self.pageNo = 0;
    [self startLoadingViewWithText:@"正在读取预约记录"];
    [self loadDataFromServer:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)loadDataFromServer:(BOOL)refresh
{
    if (refresh) {
        self.pageNo = 0;
    }
    [[WetlandService new] getOrderListForKey:self.searchBar.text PagetNo:self.pageNo+1 Success:^(NSDictionary *data) {
        if (refresh) {
            self.dataList = [NSArray array];
        }
        self.pageNo = self.pageNo + 1;
        self.dataList = [self.dataList arrayByAddingObjectsFromArray:[data objectForKey:@"pageList"]];
        [self.table reloadData];
        [self stopLoadingView];
        [self.ptr endRefresh];
        if (self.pageNo >= [[data objectForKey:@"totalPages"] integerValue]) {
            self.ptr.enablePullToBottom = NO;
        } else {
            self.ptr.enablePullToBottom = YES;
        }
    } Failure:^(NSString *msg) {
        [self showAlertWithTitle:@"错误" Msg:msg];
        [self stopLoadingView];
        [self.ptr endRefresh];
    }];
}

#pragma mark - Pull Delegate
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction
{
    if (direction == MSRefreshDirectionTop) {
        [self loadDataFromServer:YES];
    } else {
        [self loadDataFromServer:NO];
    }
}

#pragma mark - Search Delegate
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    
    [self startLoadingViewWithText:@"正在读取预约记录"];
    [self loadDataFromServer:YES];
}

#pragma  mark - Talbe Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (self.dataList?self.dataList.count:0);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BookingOrderListCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"BookingOrderListCell" owner:self options:nil] firstObject];
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    NSString *state = [self getState:[dic objectForKey:@"state"]] ;
//    if ([state isEqualToString:@"0"]) {
//        state = @"未取票";
//    }else if([state isEqualToString:@"1"]) {
//        state = @"已取票";
//    }else {
//        state = @"已过期";
//    }
    cell.titleLabel.text = [NSString stringWithFormat:@"%@(%@)", [dic objectForKey:@"presellid"], state];
    cell.dateLabel.text = [[dic objectForKey:@"createTime"] substringToIndex:10];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = [self.dataList objectAtIndex:indexPath.row];
    //跳转到订单详情
    WetLandOrderDetailViewController *detailVC = [[WetLandOrderDetailViewController alloc] initWithNibName:@"WetLandOrderDetailViewController" bundle:nil];
    detailVC.orderId =   [dic objectForKey:@"id"];
    detailVC.state =   [dic objectForKey:@"state"];
    detailVC.presellid =[dic objectForKey:@"presellid"]  ;
    detailVC.config = [NSDictionary dictionaryWithObjectsAndKeys:[dic objectForKey:@"presellid"], @"presellid", [dic objectForKey:@"state"], @"state", [dic objectForKey:@"createTime"], @"createTime", nil];
    [self.navigationController pushViewController:detailVC animated:YES];
}
-(NSString*)getState:(NSString*)state{
    if ([state isEqualToString:@"0"]) {
        state = @"未取票";
    }else if([state isEqualToString:@"1"]) {
        state = @"已取票";
    }else {
        state = @"已过期";
    }
    return state;
}
@end
